package com.quentinbeauperin.twitbird
{
	import flash.display.Bitmap;
	import flash.display.Loader;
	import flash.display.LoaderInfo;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.ProgressEvent;
	import flash.net.URLRequest;
	
	[SWF(width="800", height="450")]
	
	public class Preloader extends Sprite
	{
		public var preloaderFront:Bitmap;
		public var preloaderMiddle:Bitmap;
		public var preloaderBack:Bitmap;
		
		public function Preloader()
		{
			super();
			
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(event:Event):void {
			// Scaling
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			preloaderFront = new Assets.PreloaderFront();
			preloaderMiddle = new Assets.PreloaderMiddle();
			preloaderBack = new Assets.PreloaderBack();
			
			addChild(preloaderBack);
			addChild(preloaderMiddle);
			addChild(preloaderFront);
			
			var loader : Loader = new Loader();
			loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, handleProgress);
			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, handleComplete);
			loader.load(new URLRequest("twitbird.swf"));
		}
		
		private function handleProgress(event:ProgressEvent):void
		{
			preloaderMiddle.y = (event.bytesLoaded / event.bytesTotal) * 130
		}
		
		private function handleComplete(event:Event):void
		{
			addChild((event.target as LoaderInfo).content);
		}
	}
}